package android;


import hibernate.HibernateSessionFactory;
import hibernate.Orderstab;

import hibernate.Parkstab;


import java.io.IOException;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;





import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Servlet implementation class SearchParkingServlet
 */

public class GetAllOrdersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetAllOrdersServlet() {
		super();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		request.setCharacterEncoding("utf-8");

		String userIDstr=(String)request.getSession().getAttribute("userID");
		System.out.println("get connection in getAllOrders + userId"+userIDstr);
		int userID=Integer.valueOf(userIDstr);
		JSONObject resultJson=new JSONObject();
		Session hibernateSession=HibernateSessionFactory.getSession();
		
		try {
			
			List<Orderstab> orders=null;
			
			
			String hql="from Orderstab as o where o.driverstab.uuid = :driverID";
			
			Query result=hibernateSession.createQuery(hql);
			result.setInteger("driverID",userID);
			orders=result.list();
			resultJson.put("total",new Integer(orders.size()).toString());
			JSONArray jsonArray = new JSONArray();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			for (Orderstab o : orders) {
				
				JSONObject temp = new JSONObject();
				temp.put("uuid",o.getUuid());
				temp.put("parkUUID", o.getParkstab().getUuid().toString());
				temp.put("longitude", o.getParkstab().getLongitude().toString());
				temp.put("latitude", o.getParkstab().getLatitude().toString());
				temp.put("price", Double.toString(o.getParkstab().getPrice()));
				temp.put("orderTime",Integer.toString(o.getOrderTime()));
				temp.put("orderNumber",Integer.toString(o.getOrderNumber()));
				temp.put("pushTime", sdf.format(o.getPushTime()));
				temp.put("state", o.getState());
				
				//��ѯͣ�������
				List<Parkstab> thePark=null;
				String hqlOfPark="from Parkstab as p where p.uuid = :parkID";
				Query queryPark=hibernateSession.createQuery(hqlOfPark);
				queryPark.setInteger("parkID",Integer.valueOf(o.getParkstab().getUuid().toString()));
				thePark=queryPark.list();
				temp.put("name", thePark.get(0).getName());
				
				Timestamp tempTime;
				tempTime=o.getFinishTime();
				if(tempTime==null)
				{
					temp.put("finishTime", "null");
				}
				else
				{
					temp.put("finishTime", sdf.format(o.getFinishTime()));
				}	
				
				jsonArray.put(temp);
			}
			resultJson.put("ordersData", jsonArray);
			
			
		} catch (JSONException e1) {
			System.out.println("GetAllOrdersServlet json faile");
			e1.printStackTrace();
		}
		finally
		{
			hibernateSession.close();
		}

		OutputStream out=response.getOutputStream();
		out.write(resultJson.toString().getBytes("utf-8"));
		out.flush();
		out.close();
		System.out.println(resultJson.toString()+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
	}

}
